﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMusicStore;
using MvcMusicStore.Models;


namespace MvcMusicStore.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class AlbumManagerController : Controller
    {
        private readonly List<string> acceptableContentTypes = new List<string> { "image/jpeg", "image/pjpeg", "image/gif", "image/png" };
        private readonly string coversPath = @"~/Content/Images/";

        private readonly MusicShopDBEntities db = new MusicShopDBEntities();

        //
        // GET: /AlbumManager/
        [HttpGet]
        public ViewResult Index()
        {
            var album = db.Album.Include("Author").Include("Genre");
            return View(album.ToList());
        }

        //
        // GET: /AlbumManager/Details/5
        [HttpGet]
        public ViewResult Details(int id)
        {
            Album album = db.Album.Single(a => a.id == id);
            return View(album);
        }

        //
        // GET: /AlbumManager/Create
        [HttpGet]
        public ActionResult Create()
        {
            ViewBag.idAuthor = new SelectList(db.Author, "id", "nameAuthor");
            ViewBag.idGenre = new SelectList(db.Genre, "id", "nameGenre");
            return View();
        }

        //

        private string makeUniqueFileName(string fileName)
        {
            return string.Format(@"{0}-{1}{2}",
                                                Path.GetFileNameWithoutExtension(fileName),
                                                DateTime.Now.Ticks.ToString(),
                                                Path.GetExtension(fileName)
                                );
        }
        // POST: /AlbumManager/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Album album, HttpPostedFileBase file)
        {
            album.dateAdd = DateTime.Now;
            album.counterBought = 0;

            if (ModelState.IsValid)
            {
                if (null != file
                    && null != file.FileName
                    && Path.HasExtension(file.FileName)
                    && (0 < file.ContentLength)
                    && acceptableContentTypes.Contains(file.ContentType))
                {
                    try
                    {
                        string uniqueFileName = makeUniqueFileName(file.FileName);

                        var serverPath = Path.Combine(Server.MapPath(coversPath), uniqueFileName);
                        file.SaveAs(serverPath);

                        var clientPath = Url.Content(coversPath + uniqueFileName);

                        album.coverAlbum = clientPath;
                        db.Album.AddObject(album);
                        db.SaveChanges();

                        return RedirectToAction("Edit", new { id = album.id });
                    }
                    catch
                    {
                        ViewBag.idAuthor = new SelectList(db.Author, "id", "nameAuthor", album.idAuthor);
                        ViewBag.idGenre = new SelectList(db.Genre, "id", "nameGenre", album.idGenre);
                        return View(album);
                    }
                }
            }
            return RedirectToAction("Create");
        }


        //
        // GET: /AlbumManager/Edit/5

        public ActionResult Edit(int id)
        {
            Album album = db.Album.Single(a => a.id == id);
            ViewBag.idAuthor = new SelectList(db.Author, "id", "nameAuthor", album.idAuthor);
            ViewBag.idGenre = new SelectList(db.Genre, "id", "nameGenre", album.idGenre);
            return View(album);
        }

        //
        // POST: /AlbumManager/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Album album, HttpPostedFileBase file)
        {
            album.dateAdd = DateTime.Now;

            if (ModelState.IsValid)
            {
                if (null != file
                    && null != file.FileName
                    && Path.HasExtension(file.FileName)
                    && (0 < file.ContentLength)
                    && acceptableContentTypes.Contains(file.ContentType))
                {
                    try
                    {
                        string uniqueFileName = makeUniqueFileName(file.FileName);

                        var serverPath = Path.Combine(Server.MapPath(coversPath), uniqueFileName);
                        file.SaveAs(serverPath);

                        var clientPath = Url.Content(coversPath + uniqueFileName);

                        album.coverAlbum = clientPath;

                    }
                    catch
                    {
                        ViewBag.idAuthor = new SelectList(db.Author, "id", "nameAuthor", album.idAuthor);
                        ViewBag.idGenre = new SelectList(db.Genre, "id", "nameGenre", album.idGenre);
                        return View(album);
                    }
                }
                try
                {
                    db.Album.Attach(album);
                    db.ObjectStateManager.ChangeObjectState(album, EntityState.Modified);
                    db.SaveChanges();
                }
                catch
                {
                }
            }
            ViewBag.idAuthor = new SelectList(db.Author, "id", "nameAuthor", album.idAuthor);
            ViewBag.idGenre = new SelectList(db.Genre, "id", "nameGenre", album.idGenre);
            return View(album);
        }

        //
        // GET: /AlbumManager/Delete/5

        public ActionResult Delete(int id)
        {
            Album album = db.Album.Single(a => a.id == id);
            return View(album);
        }

        //
        // POST: /AlbumManager/Delete/5


        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Album album = db.Album.Single(a => a.id == id);
            if (album.counterBought == 0)
            {
                db.Album.DeleteObject(album);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return View("NoDelete", album);
            }

        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
